package com.admin.web.controller;

import com.admin.web.uresponse.OBGUserResponse;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.foodBox.FoodBox;
import com.bayss.core.service.food.FoodBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 餐盒管理
 */
@RestController
public class FoodBoxController  extends ApiBaseController{
    @Autowired
    private FoodBoxService foodBoxService;

    /**
     * 餐盒删除
     * @param id
     * @return
     */
    @GetMapping(value = "/foodBox/delete.json")
    public ResultMsg deleteById(Integer id) {
        try {
            foodBoxService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResultMsg.fail(-1, "");
        }
        return ResultMsg.success();
    }

    /**
     * 餐盒新增
     * @param foodBox
     * @return
     */
    @PostMapping(value = "/foodBox/add.json")
    public ResultMsg add(OBGUserResponse obgUserResponse, @RequestBody FoodBox foodBox) {
        foodBox.setOperatorId(obgUserResponse.getUid());
        return foodBoxService.add(foodBox);
    }

    /**
     * 餐盒更新
     * @param foodBox
     * @return
     */
    @PostMapping(value = "/foodBox/update.json")
    public ResultMsg update(OBGUserResponse obgUserResponse, @RequestBody FoodBox foodBox) {
        foodBox.setOperatorId(obgUserResponse.getUid());
        foodBoxService.update(foodBox);
        return ResultMsg.success();
    }

    /**
     * 餐盒分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/foodBox/getByPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize) {
        try {
            return ResultMsg.success(foodBoxService.findByPage(page, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "");
        }
    }

    /**
     * 餐盒查询所有不分页
     * @return
     */
    @GetMapping(value = "/foodBox/list.json")
    public ResultMsg list( ) {
        try {
            return ResultMsg.success(foodBoxService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "");
        }
    }
}
